package com.ecom.common.dao;

import com.ecom.common.model.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import sun.util.logging.resources.logging;

import javax.sql.DataSource;

/**
 * Created by jimmy on 7/28/14.
 */
@Repository
public class UserDao {

    private JdbcTemplate jdbcTemplate;
    private static final Logger logger = Logger.getLogger(UserDao.class);

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }


    public boolean verifyUser(User user) {
        boolean isSuccess = false;

        logger.debug("username is: " + user.getUsername());
        String sqlString = "select 1 from user_account where e_mail = ? and password = ?";

        try {
            int value = jdbcTemplate.queryForInt(sqlString, new Object[]{user.getUsername(), user.getPassword()});

            if (value > 0) {
                isSuccess = true;
            }

        } catch (EmptyResultDataAccessException e) {

        }

        return isSuccess;
    }
}